/*
 * @(#)AttributeUtils.java   1.0   May 18, 2007
 *
 * Copyright 2000-2007 ETH Zurich. All Rights Reserved.
 *
 * This software is the proprietary information of ETH Zurich.
 * Use is subject to license terms.
 *
 * @(#) $Id$
 */
package ch.ethz.globis.javaom.utils;

import ch.ethz.globis.javaom.core.AttributeValue;
import ch.ethz.globis.javaom.core.Bulk;
import ch.ethz.globis.javaom.core.Object;
import ch.ethz.globis.javaom.core.Tuple;
import ch.ethz.globis.javaom.system.Metadata;

public class AttributeUtils {

   public static Bulk getAttributeValues(final Bulk attributeValues, final Object attribute) {
      final Bulk result = new Bulk();
      Tuple tuple;
      for (Object current : attributeValues) {
         tuple = (Tuple) current;
         if (tuple.domain().equals(attribute)) {
            result.add(tuple.range());
         }
      }
      return result;
   }

   @SuppressWarnings("unchecked")
   public static AttributeValue<String> getName(final Object attribute) {

      final Bulk argumentHasAttributeValues = ObjectUtils.getAttributeValues(attribute);

      // get name attribute object
      final Object nameAttribute = Metadata.METADATA.getAttributeObject(Metadata.ATTRIBUTE,
            Metadata.NAME);

      // get links of name attribute
      final Bulk nameValues = AttributeUtils.getAttributeValues(argumentHasAttributeValues,
            nameAttribute);

      // test if not empty
      if (nameValues.isEmpty()) {
         throw new RuntimeException("Attribute '" + attribute.toString()
               + "' given has no name attribute value");
      }
      // test if no more than one value
      if (nameValues.size() > 1) {
         throw new RuntimeException("Attribute '" + attribute.toString()
               + "' given has more than one name attribute value");
      }

      // return result
      try {
         return (AttributeValue<String>) nameValues.iterator().next();
      } catch (final ClassCastException e) {
         throw new RuntimeException("Attribute '" + attribute.toString()
               + "' given has a name attribute value of type '"
               + nameValues.iterator().next().getClass().getSimpleName()
               + "' instead of AttributeValue<String>");
      }
   }

   public static Object getType(final Object attribute) {

      final Bulk argumentHasAttributeValues = ObjectUtils.getAttributeValues(attribute);

      // get type attribute object
      final Object typeAttribute = Metadata.METADATA.getAttributeObject(Metadata.ATTRIBUTE,
            Metadata.TYPE);

      // get links of type attribute
      final Bulk typeValues = AttributeUtils.getAttributeValues(argumentHasAttributeValues,
            typeAttribute);

      // test if not empty
      if (typeValues.isEmpty()) {
         throw new RuntimeException("Attribute '" + attribute.toString()
               + "' given has no type attribute value");
      }
      // test if no more than one value
      if (typeValues.size() > 1) {
         throw new RuntimeException("Attribute '" + attribute.toString()
               + "' given has more than one type attribute value");
      }

      // return result
      return typeValues.iterator().next();
   }
}
